<?php

class gameComponents extends sfComponents {

    function executeAllTournaments(sfWebRequest $request) {
        $c = new Criteria();
        $c->addDescendingOrderByColumn(TournamentPeer::UPDATED_AT);
        $this->pager = new PropelPager($c, 'TournamentPeer', 'doSelect', $this->Page, 12);
    }

    function executeGameProfile(sfWebRequest $request) {
        $this->oActivPlayer = $this->getUser()->getActivPlayer();
        $this->oActMatch = null;
        if ($this->oActivPlayer) {
            $this->oTournament = $this->oActivPlayer->getTournament();
            if ($this->oTournament->getStatus() == Tournament::$GAME_STATUS_PLAYING) {

                $c = new Criteria();
                $c->add(GameMatchPeer::STATUS, GameMatch::$GAMEMATCH_STATUS_START);
                $criterion1 = $c->getNewCriterion(GameMatchPeer::FIRST_PLAYER_ID, $this->oActivPlayer->getId(), Criteria::EQUAL);
                $criterion2 = $c->getNewCriterion(GameMatchPeer::SECOND_PLAYER_ID, $this->oActivPlayer->getId(), Criteria::EQUAL);
                $criterion1->addOr($criterion2);
                $c->add($criterion1);
                $c->addDescendingOrderByColumn(GameMatchPeer::ID);
                $this->oActMatch = GameMatchPeer::doSelectOne($c);

                $this->bFakeMatch=false;
                
                
                if (!$this->oActMatch)
                {
                    $this->bFakeMatch=true;
                    $c = new Criteria();
                    $c->add(GameMatchPeer::STATUS, GameMatch::$GAMEMATCH_STATUS_ENDED);
                    $criterion1 = $c->getNewCriterion(GameMatchPeer::FIRST_PLAYER_ID, $this->oActivPlayer->getId(), Criteria::EQUAL);
                    $criterion2 = $c->getNewCriterion(GameMatchPeer::SECOND_PLAYER_ID, $this->oActivPlayer->getId(), Criteria::EQUAL);
                    $criterion1->addOr($criterion2);
                    $c->add($criterion1);
                    $c->addDescendingOrderByColumn(GameMatchPeer::ID);
                    $this->oActMatch = GameMatchPeer::doSelectOne($c);
                }
                
                
                if ($this->oActMatch) {
                    $iSecondPlayerId = 0;

                    $this->iMatchCount = GameMatchPeer::getMatchCount($this->oTournament->getId());
                    $this->iGameCount = GamePeer::getGameCount($this->oActMatch->getId());
                    $this->iFirstplayer = false;
                    $this->iRealFirstPlayer = false;
                    
                    if ($this->oActMatch->getFirstPlayerId() == $this->oActivPlayer->getId()) {
                        $iSecondPlayerId = $this->oActMatch->getSecondPlayerId();
                        $this->iRealFirstPlayer=true;
                    } else {
                        $iSecondPlayerId = $this->oActMatch->getFirstPlayerId();
                    }



                    $criteria = new Criteria ();
                    $criteria->add(ActivPlayerPeer::ID, $iSecondPlayerId);
                    $v = ActivPlayerPeer::doSelectJoinAll($criteria);

                    $this->oSecActivPlayer = $v[0];

                    $this->ruleData = unserialize($this->oTournament->getRules()->getData());
                    $this->isMatrix=$this->ruleData->getType()==gameRules::$RT_MATRIX;
                    $this->iFirstplayer = $this->getUser()->isFirstGamePlayer($this->ruleData, $this->iMatchCount, $this->iGameCount, (($this->oActMatch->getFirstPlayerId() == $this->oActivPlayer->getId()) ? 1 : 0));

                    $c = new Criteria();
                    $c->addSelectColumn(ScorePeer::PLAYER_ID);
                    $c->addSelectColumn('SUM(' . ScorePeer::SCORE . ') as cnt');
                    $c->addSelectColumn(ScorePeer::PLAYER_ID);
                    $c->add(ScorePeer::MATCH_ID, $this->oActMatch->getId(), Criteria::IN);
                    $c->add(ScorePeer::PLAYER_ID, array($iSecondPlayerId, $this->oActivPlayer->getId()), Criteria::IN);
                    $c->addGroupByColumn(ScorePeer::PLAYER_ID);
                    $res = ScorePeer::doSelectStmt($c);

                    $ranking = array();

                    foreach ($res->fetchAll() as $node) {
                        $ranking[$node[0]] = $node[1];
                    }

                    $this->iSecPlayerScore = array(isset($ranking[$this->oActivPlayer->getId()]) ? $ranking[$this->oActivPlayer->getId()] : 0, isset($ranking[$iSecondPlayerId]) ? $ranking[$iSecondPlayerId] : 0);
                    
                    
                    
                    
                    $this->iTournamentScore=array(
                        ScorePeer::getTournamentScore($this->oTournament->getId(),$this->oActivPlayer->getId()),
                        ScorePeer::getTournamentScore($this->oTournament->getId(),$iSecondPlayerId)
                        );
                    
                    $c = new Criteria();
                    $c->add(ScorePeer::MATCH_ID, $this->oActMatch->getId());
                    $c->addAscendingOrderByColumn(ScorePeer::GAME_ID);
                    $c->addDescendingOrderByColumn(ScorePeer::PLAYER_ID);
                    $core = ScorePeer::doSelect($c);

                    $players_win = array(array(0, 0), array(0, 0));
                    $res = array();
                    $this->iWaitWinId = 0;
                    if ($this->ruleData->getWinPoints() == gameRules::$GW_Less) {

                        foreach ($core as $co) {

                            if (isset($res[$co->getGameId()])) {
                                if ($res[$co->getGameId()][1] > $co->getScore()) {
                                    $players_win[0][0]++;
                                    $players_win[0][1] = $co->getPlayerId();
                                    
                                    $res[$co->getGameId()][3] = 0;
                                    $res[$co->getGameId()][6] = 1;
                                    
                                    //$res[$co->getGameId()][3] = $players_win[0][0];
                                    //$res[$co->getGameId()][6] = $players_win[1][0];
                                } else if ($res[$co->getGameId()][1] < $co->getScore()) {
                                    $players_win[1][0]++;
                                    $players_win[1][1] = $co->getPlayerId();

                                    $res[$co->getGameId()][3] = 0;
                                    $res[$co->getGameId()][6] = 1;
                                    
                                   // $res[$co->getGameId()][3] = $players_win[1][0];
                                   // $res[$co->getGameId()][6] = $players_win[0][0];
                                }else{
                                    $res[$co->getGameId()][3] = 0.5;
                                    $res[$co->getGameId()][6] = 0.5;
                                }
                                $res[$co->getGameId()][4] = $co->getPlayerId();
                                $res[$co->getGameId()][5] = $co->getScore();
                                $res[$co->getGameId()][7] = $co->getPlayerId();
                            } else {
                                $this->iWaitWinId = $co->getPlayerId();
                                $res[$co->getGameId()] = array($co->getPlayerid(), $co->getScore(), $co->getOption(), 0, 0, 0, 0, 0, 0);
                            }
                        }
                    } else {
                        foreach ($core as $co) {
                            if (isset($res[$co->getGameId()])) {
                                if ($res[$co->getGameId()][1] < $co->getScore()) {
                                    $players_win[0][0]++;
                                    $players_win[0][1] = $co->getPlayerId();
                                    
                                    $res[$co->getGameId()][3] = 0;
                                    $res[$co->getGameId()][6] = 1;
                                    
                                    //$res[$co->getGameId()][3] = $players_win[0][0];
                                    //$res[$co->getGameId()][6] = $players_win[1][0];
                                } else if ($res[$co->getGameId()][1] > $co->getScore()) {
                                    $players_win[1][0]++;
                                    $players_win[1][1] = $co->getPlayerId();
                                    
                                    $res[$co->getGameId()][3] = 1;
                                    $res[$co->getGameId()][6] = 0;
                                    //$res[$co->getGameId()][3] = $players_win[1][0];
                                    //$res[$co->getGameId()][6] = $players_win[0][0];
                                }else{
                                    $players_win[0][0]+=0.5;
                                    $players_win[1][0]+=0.5;
                                    $res[$co->getGameId()][3] = 0.5;
                                    $res[$co->getGameId()][6] = 0.5;
                                }
                                
                                $res[$co->getGameId()][4] = $co->getPlayerId();
                                $res[$co->getGameId()][5] = $co->getScore();
                                $res[$co->getGameId()][7] = $co->getPlayerId();
                                $this->iWaitWinId = 0;
                                //
                            } else {
                                $this->iWaitWinId = $co->getPlayerid();
                                $res[$co->getGameId()] = array($co->getPlayerid(), $co->getScore(), $co->getOption(), 0, 0, 0, 0, 0, 0);
                            }
                        }
                    }

                    $this->game_list = $res;

                    if ($players_win[1][1] == $this->oActivPlayer->getId()) {
                        $this->iSecPlayerWins = array(isset($players_win[0][0]) ? $players_win[0][0] : '0', isset($players_win[1][0]) ? $players_win[1][0] : '0');
                    } else {
                        $this->iSecPlayerWins = array(isset($players_win[1][0]) ? $players_win[1][0] : '0', isset($players_win[0][0]) ? $players_win[0][0] : '0');
                    }
                    
                    $c_com_co = new Criteria();
                    $c_com_co->add(gamePeer::MATCH_ID, $this->oActMatch->getId());
                    $c_com_co->add(gamePeer::STATUS, game::$GAME_STATUS_ENDED, Criteria::NOT_EQUAL);
                    $c_com_co->addDescendingOrderByColumn(gamePeer::CREATED_AT);
                    $this->oActGame = gamePeer::doSelectOne($c_com_co);
                    
                   // $this->dupas=  var_dump($this->oActGame);
                    
                    //$this->oActGame=null;
                }
                
                if($this->bFakeMatch)
                {
                    $this->oActMatch=null;
                }
                
            }
        }
    }

    function executeChooseTournament(sfWebRequest $request) {
        $this->aTournamentList = TournamentPeer::getStarted();
    }

}
